package cn.findviewbydream.evil.app.controller

import cn.findviewbydream.evil.app.service.OrderService
import cn.findviewbydream.evil.main.entity.BaseSeatEntity
import cn.findviewbydream.evil.main.entity.OrderEntity
import cn.findviewbydream.evil.main.entity.OrderItemEntity
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import java.util.*

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.app.controller
 * 时间：16-12-28
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：包含与android端的start和orderList两个模块
 ********************************************/
@RestController
class OrderController(@Autowired val orderService: OrderService)
{
    /**
     * 获得空闲座位列表
     */
    @RequestMapping("/app/get/seat/list")
    fun getSeatList(): List<BaseSeatEntity>? = orderService.getSeatList()

    /**
     * 获得未完成的订单列表
     */
    @RequestMapping("/app/get/order/un/list")
    fun getUnFinishOrderList(): List<OrderEntity>? = orderService.getUnFinishOrderList()

    /**
     * 获得已经完成的订单列表
     */
    @RequestMapping("/app/get/order/list")
    fun getFinishOrderList(start: Date, end: Date): List<OrderEntity>?
            = orderService.getFinishOrderList(start, end)

    /**
     * 获得订单项目列表通过订单id
     */
    @RequestMapping("/app/get/order/item/list")
    fun getOrderItemListByOrderId(orderId: Int): List<OrderItemEntity>?
            = orderService.getOrderItemListByOrderId(orderId)

    /**
     * 修改订单中订单项目的数量
     *
     * @param orderId  用于更新订单总钱数 订单id
     * @param json     List<OrderItemEntity> Json
     */
    @RequestMapping("/app/post/order/item/list/num", method = arrayOf(RequestMethod.POST))
    fun postOrderItemListNum(orderId: Int, json: String): String?
    {
        try
        {
            return orderService.setOrderItemListNum(orderId, json).toString()
        }
        catch (e: Exception)
        {
            return e.message
        }
    }

    /**
     * 将订单置为失效（非删除）
     * active 为false
     * remark 为失效
     * list 删除
     */
    @RequestMapping("/app/post/order/item/delete", method = arrayOf(RequestMethod.POST))
    fun postOrderItemDelete(orderId: Int): String?
    {
        try
        {
            return orderService.setOrderItemDelete(orderId).toString()
        }
        catch (e: Exception)
        {
            return e.message
        }
    }

    /**
     *
     */
    @RequestMapping("/app/post/finish/order", method = arrayOf(RequestMethod.POST))
    fun postFinishOrder(json: String): String?
    {
        try
        {
            return orderService.setFinishOrder(json).toString()
        }
        catch (e: Exception)
        {
            return e.message
        }
    }

}